/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.bannersample.util.NumberUtil;
import ohos.agp.components.Component;

/**
 * ForegroundToBackgroundTransformer
 *
 * @since 2021-08-05
 */
public class ForegroundToBackgroundTransformer implements BannerViewPager.PageTransformer {
    private static final float NUM_F_0_5 = 0.5F;
    float height;
    float width;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        height = (float) page.getHeight();
        width = (float) page.getWidth();
        switch (pageType) {
            case PREVIOUS:
                if (offsetPixels > 0) {
                    initOther(page);
                } else {
                    float scale = min(Math.abs(NumberUtil.sub(1.0F, 1 - offset)), NUM_F_0_5);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                    page.setPivotX(NumberUtil.mul(width, NUM_F_0_5));
                    page.setPivotY(NumberUtil.mul(height, NUM_F_0_5));
                }
                break;
            case CURRENT:
                if (offsetPixels > 0) {
                    float scale = min(Math.abs(NumberUtil.sub(1.0F, offset)), NUM_F_0_5);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                } else {
                    initOther(page);
                }

                break;
            case NEXT:
                if (offsetPixels > 0) {
                    page.setScaleX(1f);
                    page.setScaleY(1f);
                    page.setPivotX(page.getWidth());
                    page.setPivotY(page.getHeight());
                } else {
                    float scale2 = min(1.0F, NUM_F_0_5);
                    page.setScaleX(scale2);
                    page.setScaleY(scale2);
                    page.setPivotX(NumberUtil.mul(width, NUM_F_0_5));
                    page.setPivotY(NumberUtil.mul(height, NUM_F_0_5));
                }
                break;
            case OTHER:
                initOther(page);
                break;
            default:
                break;
        }
    }

    /**
     * initOther
     *
     * @param page
     */
    private void initOther(Component page) {
        page.setScaleX(1F);
        page.setScaleY(1F);
        page.setPivotX(NUM_F_0_5);
        page.setPivotY(height);
    }

    /**
     * min
     *
     * @param value float
     * @param mva float
     * @return float
     */
    static float min(float value, float mva) {
        return Math.max(value, mva);
    }
}